package com.geline.easyexcel.manager;

import com.geline.easyexcel.annotation.ExcelDropDown;
import com.geline.easyexcel.domain.ParentDict;
import com.geline.easyexcel.interfaces.DropDownHandler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: mengxin
 * @date: 2024/1/4 17:40
 */
public class DropDownService {

    private DropDownHandler handler;

    public DropDownService(DropDownHandler handler){
        this.handler = handler;
    }

    /**
     * 解析选定来源
     * @author cheng-qiang
     * @date 2022/11/9 15:29
     * @param dropDown
     * @return java.lang.String[]
     */
    public List<ParentDict> getDataObject(ExcelDropDown dropDown) {
        if (dropDown == null) {
            return null;
        }
        String[] items = dropDown.items();
        if (items!=null && items.length>0) {
            List<ParentDict> list = new ArrayList<>();
            ParentDict bean = new ParentDict();
            bean.setDictName(dropDown.dictName());
            bean.setDictCode(dropDown.dictCode());
            bean.setItemList(Arrays.asList(items));
            list.add(bean);
            return list;
        }else {
            // 情形2：动态下拉框
            return handler.getDataObject(dropDown.dictCode(), dropDown.dictLevel());
        }
    }
}
